<template>
  <el-drawer
    v-model="visible"
    :title="subTitle"
    size="80%"
    :before-close="handleClose"
    lock-scroll
    destroy-on-close
  >
    <el-form
      :model="tagForm"
      label-width="250px"
      :rules="tagFormRules"
      ref="tagFormRefs"
    >
      <el-form-item label="店铺名称" prop="storeName">
        <el-input v-model="tagForm.storeName" placeholder="是必填" />
      </el-form-item>
      <el-form-item label="提示标题" prop="storeTip">
        <el-input
          type="textarea"
          v-model="tagForm.storeTip"
          placeholder="是必填"
        />
      </el-form-item>
      <el-form-item label="提示描述" prop="storeDesc">
        <el-input
          type="textarea"
          v-model="tagForm.storeDesc"
          placeholder="是必填"
        />
      </el-form-item>
      <template v-for="(domain, index) in tagForm.goodSkus" :key="domain.key">
        <div class="skuItem">
          <!-- <div class="skuItem">商品{{index + 1}}</div> -->

          <el-form-item
            label="商品标题"
            :prop="'goodSkus.' + index + '.TITLE'"
            :rules="{
              required: true,
              message: '商品标题不能为空',
              trigger: 'blur',
            }"
          >
            <el-input v-model="domain.TITLE" placeholder="是必填" />
          </el-form-item>
          <el-form-item
            label="图片在线链接"
            :prop="'goodSkus.' + index + '.LINK'"
            :rules="{
              required: true,
              message: '图片在线链接不能为空',
              trigger: 'blur',
            }"
          >
            <el-input v-model="domain.LINK" placeholder="是必填" />
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item
                label="ASIN"
                :prop="'goodSkus.' + index + '.ASIN'"
                :rules="{
                  required: true,
                  message: 'ASIN不能为空',
                  trigger: 'blur',
                }"
              >
                <el-input
                  v-model="domain.ASIN"
                  @input="ASINBlur(domain, index)"
                  placeholder="按 SKU、ASIN 或任何商品详情搜索，如果一条数据默认获取ASIN赋值输入框"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="FNSKU"
                :prop="'goodSkus.' + index + '.FNSKU'"
                :rules="{
                  required: true,
                  message: 'FNSKU 不能为空',
                  trigger: 'blur',
                }"
              >
                <el-input v-model="domain.FNSKU" placeholder="是必填" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item
                label="SKU"
                :prop="'goodSkus.' + index + '.SKU'"
                :rules="{
                  required: true,
                  message: 'SKU 不能为空',
                  trigger: 'blur',
                }"
              >
                <el-input v-model="domain.SKU" placeholder="是必填" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="UPC/EAN"
                :prop="'goodSkus.' + index + '.UPCEAN'"
                :rules="{
                  required: true,
                  message: 'UPC/EAN不能为空',
                  trigger: 'blur',
                }"
              >
                <el-input v-model="domain.UPCEAN" placeholder="是必填">
                  <template #prepend>74</template>
                  <template #append>
                    <div class="poiner" @click="createNum(domain)">
                      随机生成
                    </div>
                  </template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <!-- <el-col :span="12">
              <el-form-item
                label="SKU数量"
                :prop="'goodSkus.' + index + '.skuNumber'"
                :rules="{
                  required: true,
                  message: 'SKU数量不能为空',
                  trigger: 'blur',
                }"
              >
                <el-input v-model="domain.skuNumber" placeholder="是必填" />
              </el-form-item>
            </el-col> -->
            <el-col :span="12">
              <el-form-item
                label="月销量"
                :prop="'goodSkus.' + index + '.saleNumber'"
                :rules="{
                  required: true,
                  message: '月销量不能为空',
                  trigger: 'blur',
                }"
              >
                <el-input v-model="domain.saleNumber" placeholder="是必填" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item
                label="商品金额"
                :prop="'goodSkus.' + index + '.price'"
                :rules="{
                  required: true,
                  message: '商品金额不能为空',
                  trigger: 'blur',
                }"
              >
                <el-input v-model="domain.price" placeholder="是必填" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="差额百分比"
                :prop="'goodSkus.' + index + '.percent'"
                :rules="{
                  required: true,
                  message: '差额百分比不能为空',
                  trigger: 'blur',
                }"
              >
                <el-input
                  v-model="domain.percent"
                  placeholder="是必填,填写整数"
                >
                  <template #append>
                    <div class="poiner" @click="createNum(domain)">%</div>
                  </template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="其他信息">
            <!-- <el-popover
              placement="top-start"
              title="其他信息"
              :width="200"
              trigger="hover"
              
            >
              <div class="content">

              </div>
              <template #reference> -->
            <div class="poiner" @click="initOther(domain)">
              <el-button type="primary"
                >基于上面信息，点我自动生成以下其他信息</el-button
              >
            </div>
            <!-- <el-button class="m-2">Hover to activate</el-button> -->
            <!-- </template> -->
            <!-- </el-popover> -->
          </el-form-item>

          <!-- //------------------------------------------ -->

          <template v-if="domain.initOtherData">
            <el-row>
              <el-col :span="12">
                <el-form-item
                  label="US（美金）"
                  :prop="'goodSkus.' + index + '.US'"
                  :rules="{
                    required: true,
                    message: 'US(美金)不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input v-model="domain.US" placeholder="是必填" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="件数"
                  :prop="'goodSkus.' + index + '.quantity'"
                  :rules="{
                    required: true,
                    message: '件数不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input v-model="domain.quantity" placeholder="是必填" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item
                  label="售出率(黑色数字)"
                  :prop="'goodSkus.' + index + '.soldRateOne'"
                  :rules="{
                    required: true,
                    message: '售出率(黑色数字)为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input v-model="domain.soldRateOne" placeholder="是必填" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="售出率(绿色数字)"
                  :prop="'goodSkus.' + index + '.soldRateTwo'"
                  :rules="{
                    required: true,
                    message: '售出率(绿色数字)不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input v-model="domain.soldRateTwo" placeholder="是必填" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item
                  label="可用"
                  :prop="'goodSkus.' + index + '.useNumber'"
                  :rules="{
                    required: true,
                    message: '可用不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input v-model="domain.useNumber" placeholder="是必填" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="入库数量（总数）"
                  :prop="'goodSkus.' + index + '.receiptQuantityTotal'"
                  :rules="{
                    required: true,
                    message: '入库数量（总数）为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input
                    v-model="domain.receiptQuantityTotal"
                    placeholder="是必填"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item
                  label="入库数量（已发货）"
                  :prop="'goodSkus.' + index + '.receiptQuantitySend'"
                  :rules="{
                    required: true,
                    message: '入库数量（已发货）不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input
                    v-model="domain.receiptQuantitySend"
                    placeholder="是必填"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="入库数量（正在接收）"
                  :prop="'goodSkus.' + index + '.receiptQuantityReceive'"
                  :rules="{
                    required: true,
                    message: '入库数量（正在接收）不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input
                    v-model="domain.receiptQuantityReceive"
                    placeholder="是必填"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item
                  label="预留数量（总额）"
                  :prop="'goodSkus.' + index + '.reservedQuantityReceive'"
                  :rules="{
                    required: true,
                    message: '预留数量（总额）不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input
                    v-model="domain.reservedQuantityReceive"
                    placeholder="是必填"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="预留数量（买家订单）"
                  :prop="'goodSkus.' + index + '.reservedQuantityReceiveBuy'"
                  :rules="{
                    required: true,
                    message: '预留数量（买家订单）不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input
                    v-model="domain.reservedQuantityReceiveBuy"
                    placeholder="是必填"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item
                  label="预留数量（亚马逊运营中心运转）"
                  :prop="'goodSkus.' + index + '.reservedQuantityReceiveYMX'"
                  :rules="{
                    required: true,
                    message: '预留数量（亚马逊运营中心运转）不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input
                    v-model="domain.reservedQuantityReceiveYMX"
                    placeholder="是必填"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="预留数量（亚马逊运营中心处理中）"
                  :prop="'goodSkus.' + index + '.reservedQuantityReceiveYMXIng'"
                  :rules="{
                    required: true,
                    message: '预留数量（亚马逊运营中心处理中）',
                    trigger: 'blur',
                  }"
                >
                  <el-input
                    v-model="domain.reservedQuantityReceiveYMXIng"
                    placeholder="是必填"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item
                  label="不可售（总额）"
                  :prop="'goodSkus.' + index + '.NotAvailableTotal'"
                  :rules="{
                    required: true,
                    message: '不可售（总额）不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input
                    v-model="domain.NotAvailableTotal"
                    placeholder="是必填"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="不可售（在库房出现残缺）"
                  :prop="'goodSkus.' + index + '.NotAvailableTotalQc'"
                  :rules="{
                    required: true,
                    message: '不可售（在库房出现残缺）不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input
                    type="number"
                    :max="3"
                    @blur="NotAvailableTotalQcBlur(domain)"
                    v-model="domain.NotAvailableTotalQc"
                    placeholder="是必填"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item
                  label="不可售（买家导致残）"
                  :prop="'goodSkus.' + index + '.NotAvailableTotalBugQc'"
                  :rules="{
                    required: true,
                    message: '不可售（买家导致残）不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input
                    v-model="domain.NotAvailableTotalBugQc"
                    placeholder="是必填"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="不可售（存在瑕疵）"
                  :prop="'goodSkus.' + index + '.NotAvailableTotalCZX'"
                  :rules="{
                    required: true,
                    message: '不可售（存在瑕疵）不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input
                    v-model="domain.NotAvailableTotalCZX"
                    placeholder="是必填"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item
                  label="库龄（0-60）"
                  :prop="'goodSkus.' + index + '.stockage06'"
                  :rules="{
                    required: true,
                    message: '库龄（0-60）不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input v-model="domain.stockage06" placeholder="是必填" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="库龄（61-90）"
                  :prop="'goodSkus.' + index + '.stockage69'"
                  :rules="{
                    required: true,
                    message: '库龄（61-90）不能为空',
                    trigger: 'blur',
                  }"
                >
                  <el-input v-model="domain.stockage69" placeholder="是必填" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="不可售(因承运人导致的残损)">
                  <el-input v-model="domain.temp002" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="不可售(因分销商导致的残损)">
                  <el-input v-model="domain.temp003" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="不可售(已过期)">
                  <el-input v-model="domain.temp004" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="预计冗余商品数量">
                  <el-input v-model="domain.temp005" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="预计超龄库存附加费">
                  <el-input v-model="domain.temp006" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="每件售出商品预计费用">
                  <el-input v-model="domain.temp007" />
                </el-form-item>
              </el-col>
            </el-row>
          </template>

          <el-form-item label="操作当前条目" v-if="tagForm.goodSkus.length > 1">
            <el-button type="danger" @click="deleteItem(index)">删除</el-button>
            <!-- <el-input v-model="domain.temp006" /> -->
          </el-form-item>
        </div>
      </template>
      <el-form-item label="" class="addbtn" v-if="!editid">
        <div class="addbtn1">
          <el-button type="primary" @click="onSubmit3">继续添加</el-button>
        </div>
      </el-form-item>
      <!-- <div></div> -->
    </el-form>
    <template #footer>
      <div class="btns">
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button type="primary" @click="onSubmit2">只更新店铺信息</el-button>
        <el-button type="primary" @click="onSubmit4">清除缓存按钮</el-button>
        <el-button type="primary" @click="onSubmit5" v-if="!editid">导入</el-button>
        <el-button type="primary" @click="onSubmit6" v-else>导出</el-button>
        <!--  -->
        <el-button @click="handleClose">取消</el-button>
      </div>
    </template>

    <input type="file" name="file" id="uploadFileRef" style="visibility: hidden;width:0;height:0" @change="uploadPhoto" >
  
    <a :href="aHref" ref="downloadRef" download="exportData.json"></a>
  </el-drawer>
</template>

<script>
import { formErrorTip, successInfo } from "@/utils/util.js";
import { appState } from "@/store/index";
const appstate = appState();
export default {
  props: {
    placeholder: {
      type: String,
      default: "请输入",
    },
    visible: {
      type: Boolean,
      default: false,
    },
    updatecount: {
      type: Number,
      default: 0,
    },
    initData: {
      type: Array,
      default: function () {
        return [];
      },
    },
    item: {
      type: Object,
      default: function () {
        return {
          TITLE: "", // 商品标题
          LINK: "", // 商品在线链接
          ASIN: "", // ASIN不能为空
          FNSKU: "", // FNSKU 不能为空
          SKU: "", // SKU
          UPCEAN: "", // UPC/EAN不能为空
          skuNumber: "", // SKU数量
          saleNumber: "", //  月销量
          price: "", // 商品金额
          percent: 10, // 差额百分比
          initOtherData: false,

          //------------------
          US: "", // US 美金
          quantity: "", // 件数
          soldRateOne: "", // 售出率(黑色数字)
          soldRateTwo: "", // 售出率(绿色数字)不能为空
          useNumber: "", // 可用
          receiptQuantityTotal: "", // 入库数量（总数）
          receiptQuantitySend: "", // 入库数量（已发货）
          receiptQuantityReceive: "", // 入库数量（正在接收）
          reservedQuantityReceive: "", // 预留数量（总额）
          reservedQuantityReceiveBuy: "", // 预留数量（买家订单）
          reservedQuantityReceiveYMX: "", // 预留数量（亚马逊运营中心运转）
          reservedQuantityReceiveYMXIng: 0, // 预留数量（亚马逊运营中心处理中）
          NotAvailableTotal: "", // 不可售（总额）
          NotAvailableTotalQc: 0, // 不可售（在库房出现残缺
          NotAvailableTotalBugQc: "", // 不可售（买家导致残）
          NotAvailableTotalCZX: "", // 不可售（存在瑕疵）
          stockage06: "", // 库龄（0-60）
          stockage69: "", // 库龄（61-90）
          // temp001:'',// 在库房出现残损
          temp002: "", // 因承运人导致的残损
          temp003: "", // 因分销商导致的残损
          temp004: "", // 已过期

          temp005: "", // 预计冗余商品数量
          temp006: "", // 预计超龄库存附加费
          temp007: "", // 每件售出商品预计费用

          // lable: "我是label",
          // value: "",
          // key: new Date().getTime(),
          // error: "不能为空",
        };
      },
    },
    row: {
      type: Object,
      default: function () {
        return null;
      },
    },
  },

  //   总额:4
  // 在库房出现残损:0
  // 买家导致残损:3
  // 因承运人导致的残损:0
  // 因分销商导致的残损:0
  // 存在瑕疵:1
  // 已过期:0

  watch: {
    updatecount(n) {
      // this.tagForm.storeName = appstate.storeInfo.storeName;
      this.tagForm.storeTip = appstate.storeInfo.storeTip;
      this.tagForm.storeDesc = appstate.storeInfo.storeDesc;

      if (this.initData.length !== 0) {
        this.tagForm.goodSkus = [];
        this.subTitle = "编辑商品";
        this.editid = this.initData[0].uuid;
        for (let i = 0; i < this.initData.length; i++) {
          let UPCEAN = this.initData[i].UPCEAN;
          if (this.initData[i].UPCEAN.startsWith("74")) {
            UPCEAN = UPCEAN.split("74")[1];
          }
          this.tagForm.goodSkus.push({
            ...this.initData[i],
            UPCEAN,
            initOtherData: true,
            key: new Date().getTime() + i,
          });
        }
      } else {
        console.log("this.item", this.item);
        this.editid = null;
        // if (!this.editid) {
        //   appstate.clear();
        // }
        this.subTitle = "新增商品";
        this.tagForm.goodSkus = [
          {
            ...this.item,
            key: new Date().getTime(),
          },
        ];

        this.createNum(this.tagForm.goodSkus[0]);
      }
      console.log(appstate.storeInfo);

      // console.log('appstate',appstate)
      // appstate.addGood(this.item)

      // console.log('this.storeInfo',this.storeInfo)
    },
    storeInfo(n) {
      console.log("storeInfo", n);
    },
  },
  computed: {
    storeInfo() {
      return appstate.storeInfo;
    },
  },
  data() {
    return {
      aHref:'',
      editid: null,
      subTitle: "添加商品",
      tagForm: {
        storeName: "",
        storeTip: "",
        storeDesc: "",

        //
        goodSkus: [{}],
      },
      tagFormRules: {
        storeName: [
          {
            required: true,
            message: "店铺名称不能为空",
            trigger: "blur",
          },
          // {
          //   min: 3,
          //   max: 5,
          //   message: "Length should be 3 to 5",
          //   trigger: "blur",
          // },
        ],
        storeTip: [
          {
            required: true,
            message: "提示标题不能为空",
            trigger: "blur",
          },
        ],
        storeDesc: [
          {
            required: true,
            message: "提示描述不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    onSubmit6(){
      let filename = 'data.json';

      let json = {
          "storeInfo": {
          "storeName": this.tagForm.storeName,
          "storeTip": this.tagForm.storeTip,
          "storeDesc": this.tagForm.storeDesc,
        },
        "inventoryResult": this.tagForm.goodSkus
      }
      json = JSON.stringify(json)

      let blob = new Blob([
        json
      ],{
        type:'text/json'
      })

      this.aHref = window.URL.createObjectURL(blob);

      this.$nextTick(() => {
        this.$refs.downloadRef.click()

      })

      successInfo('导出成功，具体看浏览器下载内容')


    },
    uploadPhoto(e){
      console.log('e',e)
      const url = URL.createObjectURL(e.target.files[0])

      this.requestFn(url)

      e.target.value = '';
      // axios()
    },
    requestFn(url){
      const that = this;
      let xhr = new XMLHttpRequest()
      xhr.open('get',url,false)
      xhr.send(null)
      if(xhr.status >= 200 && xhr.status < 300){
        console.log('xhr.responseText',xhr.responseText)
        const json = JSON.parse(xhr.responseText)

        console.log(json.storeInfo)
        console.log(json.inventoryResult)

        this.tagForm.storeName = json.storeInfo.storeName;
        this.tagForm.storeTip = json.storeInfo.storeTip;
        this.tagForm.storeDesc = json.storeInfo.storeDesc;
        let initData = json.inventoryResult
        this.tagForm.goodSkus = []

        for (let i = 0; i < initData.length; i++) {
          let UPCEAN = initData[i].UPCEAN;
          if (initData[i].UPCEAN.startsWith("74")) {
            UPCEAN = UPCEAN.split("74")[1];
          }
          this.tagForm.goodSkus.push({
            ...initData[i],
            UPCEAN,
            initOtherData: true,
            key: new Date().getTime() + i,
          });
        }



        

        // appstate.inData(json)
      }
    },
    onSubmit5(){
      const uploadFileRef = document.getElementById('uploadFileRef');

      uploadFileRef.click()
    },
    onSubmit4() {
      const that = this;
      this.$confirm("是否清空当前页面缓存?", "提示", {
        type: "Warning",
        callback: function (is) {
          console.log("is", is);
          if (is === "confirm") {
            appstate.clear();

            successInfo("清除成功");

            that.$emit("update");
          }
        },
        // icon: markRaw(Delete),
      });
    },
    ASINBlur(item, index) {
      item.FNSKU = item.ASIN;
    },
    deleteItem(index) {
      this.tagForm.goodSkus.splice(index, 1);
    },
    onSubmit2() {
      appstate.updateStore({
        storeName: this.tagForm.storeName,
        storeTip: this.tagForm.storeTip,
        storeDesc: this.tagForm.storeDesc,
      });

      successInfo("更新成功");
      // this.close("query");
    },
    //
    async initOther(row) {
      row.initOtherData = false;

      const is = await this.validForm();
      if (!is) {
        return;
      }
      const that = this;

      this.$confirm("基于以上信息重新初始化下面相关信息?", "提示", {
        type: "Warning",
        callback: function (is) {
          console.log("is", is);
          if (is === "confirm") {
            that.initParams(row, that);
          }
        },
        // icon: markRaw(Delete),
      });
    },
    initParams(row, that) {
      row.US = that.getUS(row);
      row.quantity = that.getQuantify(row);
      row.soldRateOne = that.getSoldRateOne(row);
      row.soldRateTwo = that.getSoldRateTwo(row);
      row.useNumber = that.getUseNumber(row);
      row.receiptQuantitySend = that.getReceiptQuantitySend(row);
      row.receiptQuantityReceive = that.getReceiptQuantityReceive(row);
      row.receiptQuantityTotal = that.getReceiptQuantityTotal(row);
      row.reservedQuantityReceiveBuy = that.getReservedQuantityReceiveBuy(row);
      row.reservedQuantityReceiveYMX = that.getReservedQuantityReceiveYMX(row);
      row.reservedQuantityReceiveYMXIng = 0;

      row.NotAvailableTotalBugQc = that.getNotAvailableTotalBugQc(row);
      row.NotAvailableTotalCZX = that.getNotAvailableTotalCZX(row);

      row.NotAvailableTotal = that.getNotAvailableTotal(row);

      row.stockage06 = that.getStzockage06(row);
      row.stockage69 = that.getstockage69(row);

      // reservedQuantityReceive
      row.reservedQuantityReceive = that.getReservedQuantityReceive(row);

      row.initOtherData = true;
    },
    getNotAvailableTotal(row) {
      row.NotAvailableTotalQc = row.NotAvailableTotalQc - 0;
      row.NotAvailableTotalBugQc = row.NotAvailableTotalBugQc - 0;
      row.NotAvailableTotalCZX = row.NotAvailableTotalCZX - 0;

      return Math.floor(
        row.NotAvailableTotalQc +
          row.NotAvailableTotalBugQc +
          row.NotAvailableTotalCZX
      );
    },
    // 库龄（61-90）
    getstockage69(row) {
      row.useNumber = row.useNumber - 0;
      row.stockage06 = row.stockage06 - 0;

      return row.useNumber - row.stockage06;
    },
    // 库龄（0-60）
    getStzockage06(row) {
      row.useNumber = row.useNumber - 0;

      return Math.floor(row.useNumber * 0.85);
    },
    // 不可售（存在瑕疵）
    getNotAvailableTotalCZX(row) {
      let number = Math.floor(Math.random() * 6);
      return number;
    },
    getNotAvailableTotalBugQc(row) {
      let number = Math.floor(Math.random() * 9 + 1);
      return number;
    },
    NotAvailableTotalQcBlur(row) {
      if (row.NotAvailableTotalQc === "" || row.NotAvailableTotalQc > 3) {
        row.NotAvailableTotalQc = 0;
      }
    },
    // 预留数量（亚马逊运营中心运转）
    getReservedQuantityReceiveYMX(row) {
      let number = Math.floor(Math.random() * 40 + 10);
      return number;
    },
    // 预留数量（买家订单）
    getReservedQuantityReceiveBuy(row) {
      row.saleNumber = row.saleNumber - 0;

      return Math.floor(
        (row.saleNumber / 30) * 2 -
          (row.saleNumber / 30) * 2 * (row.percent / 100)
      );
    },
    // 预留数量（总额）
    getReservedQuantityReceive(row) {
      row.reservedQuantityReceiveBuy = row.reservedQuantityReceiveBuy - 0;
      row.reservedQuantityReceiveYMX = row.reservedQuantityReceiveYMX - 0;
      row.reservedQuantityReceiveYMXIng = row.reservedQuantityReceiveYMXIng - 0;
      return Math.floor(
        row.reservedQuantityReceiveBuy +
          row.reservedQuantityReceiveYMX +
          row.reservedQuantityReceiveYMXIng
      );
    },
    getReceiptQuantityReceive(row) {
      row.saleNumber = row.saleNumber - 0;
      return Math.floor((row.saleNumber / 30) * 7);
    },
    //
    getReceiptQuantitySend(row) {
      row.saleNumber = row.saleNumber - 0;
      // return Math.floor((row.saleNumber - 7) * (row.saleNumber / 30));
      return Math.floor((30 - 7) * (row.saleNumber / 30));
    },
    // 入库总数
    getReceiptQuantityTotal(row) {
      row.saleNumber = row.saleNumber - 0;

      // 已发货加正在接受
      // return row.receiptQuantitySend + row.receiptQuantityReceive;
      // 入库数量（已发货）需要调整公式：(月天数30-7)*(月销量/30)，例子：30天减去正在接收的天数再乘以每天销售数，尽量往整体数来填  (30-7)*36=800

      // (月天数30-7)*(月销量/30)，例子：30天减去正在接收的天数再乘以每天销售数，尽量往整体数来填  (30-7)*36=800
      // return row.receiptQuantitySend + row.receiptQuantityReceive;
      // return Math.floor((30 - 7) * (row.saleNumber / 30));
      return row.receiptQuantitySend + row.receiptQuantityReceive;
    },
    // 可用数量
    getUseNumber(row) {
      row.saleNumber = row.saleNumber - 0;
      row.percent = row.percent - 0;
      console.log(
        " (row.saleNumber + (row.saleNumber / 30) * 10) * (1 - row.percent / 100)",
        (row.saleNumber + (row.saleNumber / 30) * 10) * (1 - row.percent / 100)
      );
      return Math.floor(
        (row.saleNumber + (row.saleNumber / 30) * 10) * (1 - row.percent / 100)
      );
    },
    getSoldRateTwo(row) {
      // 0.110 - 0.399
      let number = ((Math.random() * 280 + 110) / 1000).toFixed(3);
      return number;
    },
    getSoldRateOne(row) {
      // 1000 - 1299
      let number = ((Math.random() * 299 + 1000) / 1000).toFixed(3);
      return number;
    },
    // 件数
    getQuantify(row) {
      row.saleNumber = row.saleNumber - 0;
      row.percent = row.percent - 0;

      return Math.floor(
        row.saleNumber * 3 - row.saleNumber * 3 * (row.percent / 100)
      );
    },
    getUS(row) {
      row.saleNumber = row.saleNumber - 0;
      row.price = row.price - 0;
      row.percent = row.percent - 0;
      return (
        row.saleNumber * 3 * row.price -
        row.saleNumber * 3 * row.price * (row.percent / 100)
      ).toFixed(2);
    },
    createNum(row) {
      let num = "";
      for (let i = 0; i < 10; i++) {
        num += Math.floor(Math.random() * 10);
      }
      row.UPCEAN = num;
    },
    validForm() {
      return new Promise((resolve, reject) => {
        this.$refs.tagFormRefs.validate((valid, fields) => {
          if (valid) {
            // console.log("submit!", this.tagForm);
            resolve(true);
          } else {
            resolve(false);
            // console.log("error submit!", fields);
            formErrorTip(fields);
          }
        });
      });
    },
    async onSubmit3() {
      const is = await this.validForm();
      if (!is) {
        return;
      }

      this.tagForm.goodSkus.push({
        ...this.item,
        key: new Date().getTime(),
      });

      this.createNum(this.tagForm.goodSkus[this.tagForm.goodSkus.length - 1]);
    },
    onSubmit() {
      this.$refs.tagFormRefs.validate((valid, fields) => {
        if (valid) {
          console.log("submit!", this.tagForm);
          if (!this.editid) {
            appstate.clearGood();
          }

          for (let i = 0; i < this.tagForm.goodSkus.length; i++) {
            let item = this.tagForm.goodSkus[i];
            if (!item.initOtherData) {
              this.initParams(item, this);
            }
            if (!this.editid) {
              appstate.addGood({
                ...item,
                UPCEAN: "74" + item.UPCEAN,
                uuid: new Date().getTime() + i,
                storeId: this.storeInfo.storeUUID,
              });
            } else {
              appstate.updateGood({
                ...item,
                UPCEAN: "74" + item.UPCEAN,
                uuid: this.editid,
              });
            }
          }

          appstate.updateStore({
            storeName: this.tagForm.storeName,
            storeTip: this.tagForm.storeTip,
            storeDesc: this.tagForm.storeDesc,
          });

          successInfo("添加成功");
          this.close("query");
        } else {
          console.log("error submit!", fields);
          formErrorTip(fields);
        }
      });
    },
    handleClose() {
      this.$confirm("数据将不会保存，确认关闭？")
        .then((_) => {
          this.close();
        })
        .catch((_) => {});
    },
    close(type) {
      this.$emit("close", type);
    },
  },
};
</script>

<style lang="scss" scoped>
.poiner {
  cursor: pointer;
}
.addbtn1 {
  display: flex;
  justify-content: center;
  width: 100%;
}
.addbtn {
  margin-top: 20px;
}
.btns {
  display: flex;
}
.skuItem {
  background: #e3dfde;
  padding: 20px 0;
  border-bottom: 1px solid #fff;
}
</style>